/*
 * @Author: ylyu
 * @Date: 2024-02-22 21:37:22
 * @LastEditors: ylyu
 * @LastEditTime: 2024-02-23 21:29:20
 * @Description:
 */
import { defineStore } from 'pinia'
import store from 'store2'
import _ from 'lodash'

const useWebsiteStore = defineStore('websiteStore', {
  state: () => ({
    websites: [
      {
        title: '百度一下，你就知道',
        screenshot: 'https://placekitten.com/60/60',
        url: 'https://www.baidu.com',
      },
    ],
  }),
  actions: {
    add(item) {
      console.log('action===', this)
      if (_.find({ url: item.url })) {
        return console.log('此网站已被添加')
      }
      this.websites.unshift(item)
      store('websites', this.websites)
    },
    init() {
      this.websites = store.get('websites') // 从本地存储中拿值
    },
    deleteItem(url) {
      // const res = _.findIndex(this.websites, { url })
      // _.slice(this.websites,index,1)
      this.websites = _.dropWhile(this.websites, { url })
      store('websites', this.websites)
    },
  },
})
export default useWebsiteStore
